//
//  LoginViewController.m
//  YXFinance
//
//  Created by uplooking on 2018/10/22.
//  Copyright © 2018年 CRLong. All rights reserved.
//

#import "LoginViewController.h"

@interface LoginViewController ()
<
UITextFieldDelegate
>
{
    
    UITextField * phoneTextField;
    UITextField * codeTextField;
    UITextField * passWordTextField;
    UIButton *phoneBtn;
    UILabel *label;
    NSTimer *timer;
    NSInteger count;
    UIButton *codeBtn;
    UIButton *loginBtn;
    
    UIView * codeView;
    UIView * passView;
    UIButton * typeButton;
    
}

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"登录";
    [self setBackLeftBarButtonItemAction:@selector(backAction:)];
    [self initView];
    [self setBackRightBarButtonItemAction:@selector(registerBtnAction:) text:@"注册"];
}

- (void)backAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)initView{
    
    UIImageView * imageView = [[UIImageView alloc]init];
    [self.view addSubview:imageView];
    [imageView setImage:[UIImage imageNamed:@"icon_denglu_logo"]];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(45 * kAutoScanleX);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(74 * kAutoScanleX, 74 * kAutoScanleX));
    }];
    
    
    UIImageView * midView = [[UIImageView alloc]init];
    [midView setUserInteractionEnabled:YES];
    [midView setImage:[UIImage imageNamed:@"bg_denglu_shuru_box"]];
    [self.view addSubview:midView];
    [midView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView.mas_bottom).offset(40 * kAutoScanleX);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(345 * kAutoScanleX, 123 * kAutoScanleX));
    }];

    
    
    UIView * phoneView = [[UIView alloc]init];
    [midView addSubview:phoneView];
    [phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(midView.mas_centerY);
        make.left.right.equalTo(midView);
        make.height.mas_equalTo(54 * kAutoScanleX);
    }];
    
    UIImageView * pImageView = [[UIImageView alloc]init];
    [phoneView addSubview:pImageView];
    [pImageView setImage:[UIImage imageNamed:@"icon_denglu_phone"]];
    [pImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(phoneView);
        make.left.equalTo(phoneView).offset(25 * kAutoScanleX);
        make.size.mas_equalTo(CGSizeMake(17 * kAutoScanleX, 20 * kAutoScanleX));
    }];

    
    phoneTextField=[[UITextField alloc]init];
    [phoneTextField setDelegate:self];
    [phoneView addSubview:phoneTextField];
    [phoneTextField setKeyboardType:UIKeyboardTypePhonePad];
    [phoneTextField setReturnKeyType:UIReturnKeyDone];
    [phoneTextField setFont:[UIFont systemFontOfSize:15 * kAutoScanleX]];
    [phoneTextField setValue:HexRGBA(0xa4a4a4, 1) forKeyPath:@"_placeholderLabel.textColor"];
    [phoneTextField setPlaceholder:@"请输入手机号码"];
    [phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(phoneView);
        make.left.equalTo(pImageView.mas_right).offset(15 * kAutoScanleX);
        make.right.equalTo(phoneView);
        make.height.mas_equalTo(50 * kAutoScanleX);
    }];
    
    
     codeView = [[UIView alloc]init];
    [midView addSubview:codeView];
    [codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(midView.mas_centerY);
        make.left.right.equalTo(midView);
        make.height.mas_equalTo(54 * kAutoScanleX);
    }];
    
    UIImageView * cImageView = [[UIImageView alloc]init];
    [codeView addSubview:cImageView];
    [cImageView setImage:[UIImage imageNamed:@"icon_denglu_yanzhengma"]];
    [cImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(codeView);
        make.left.equalTo(codeView).offset(25 * kAutoScanleX);
        make.size.mas_equalTo(CGSizeMake(17 * kAutoScanleX, 20 * kAutoScanleX));
    }];
    
    
    codeTextField = [[UITextField alloc]init];
    [codeTextField setDelegate:self];
    [codeView addSubview:codeTextField];
    [codeTextField setSecureTextEntry:YES];
    [codeTextField setKeyboardType:UIKeyboardTypePhonePad];
    [codeTextField setReturnKeyType:UIReturnKeyDone];
    [codeTextField setFont:[UIFont systemFontOfSize:15 * kAutoScanleX]];
    [codeTextField setValue:HexRGBA(0xa4a4a4, 1) forKeyPath:@"_placeholderLabel.textColor"];
    [codeTextField setPlaceholder:@"请输入验证码"];
    [codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(codeView);
        make.left.equalTo(cImageView.mas_right).offset(15 * kAutoScanleX);
        make.right.equalTo(codeView).offset(-110 * kAutoScanleX);
        make.height.mas_equalTo(50 * kAutoScanleX);
    }];
    
    
    label=[[UILabel alloc]init];
    [codeView addSubview:label];
    [label setText:@"获取验证码"];
    [label setBackgroundColor:HexRGBA(0xa4a4a4, 1)];
    [label setFont:[UIFont systemFontOfSize:11 * kAutoScanleX]];
    [label setTextColor:HexRGBA(0xffffff, 1)];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label.layer setMasksToBounds:YES];
    [label.layer setCornerRadius:4 * kAutoScanleX];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(codeView);
        make.right.equalTo(codeView).offset(-25 * kAutoScanleX);
        make.size.mas_equalTo(CGSizeMake(70 * kAutoScanleX, 24 * kAutoScanleX));
    }];
    
    
    codeBtn = [[UIButton alloc]init];
    [codeBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [codeView addSubview:codeBtn];
    [codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(label);
    }];


    
    passView = [[UIView alloc]init];
    [passView setHidden:YES];
    [midView addSubview:passView];
    [passView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(midView.mas_centerY);
        make.left.right.equalTo(midView);
        make.height.mas_equalTo(54 * kAutoScanleX);
    }];

    UIImageView * passImageView = [[UIImageView alloc]init];
    [passView addSubview:passImageView];
    [passImageView setImage:[UIImage imageNamed:@"icon_denglu_yanzhengma"]];
    [passImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(passView);
        make.left.equalTo(passView).offset(25 * kAutoScanleX);
        make.size.mas_equalTo(CGSizeMake(17 * kAutoScanleX, 20 * kAutoScanleX));
    }];


    passWordTextField = [[UITextField alloc]init];
    [passWordTextField setDelegate:self];
    [passWordTextField setSecureTextEntry:YES];
    [passView addSubview:passWordTextField];
    [passWordTextField setReturnKeyType:UIReturnKeyDone];
    [passWordTextField setFont:[UIFont systemFontOfSize:15 * kAutoScanleX]];
    [passWordTextField setValue:HexRGBA(0xa4a4a4, 1) forKeyPath:@"_placeholderLabel.textColor"];
    [passWordTextField setPlaceholder:@"请输入密码"];
    [passWordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(passView);
        make.left.equalTo(passImageView.mas_right).offset(15 * kAutoScanleX);
        make.right.equalTo(passView).offset(-15 * kAutoScanleX);
        make.height.mas_equalTo(50 * kAutoScanleX);
    }];
    
    
    UIButton * secureButton = [[UIButton alloc]init];
    [secureButton setImage:[UIImage imageNamed:@"icon_mima_guanbi"] forState:UIControlStateNormal];
    [secureButton setImage:[UIImage imageNamed:@"icon_mima_zhankai"] forState:UIControlStateSelected];
    [secureButton addTarget:self action:@selector(buttonSecureAction:) forControlEvents:UIControlEventTouchUpInside];
    [passView addSubview:secureButton];
    [secureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(passView).offset(-5 * kAutoScanleX);
        make.centerY.equalTo(passView);
        make.size.mas_equalTo(CGSizeMake(44 * kAutoScanleX, 44 * kAutoScanleX));
    }];
    

    UIView *line = [[UIView alloc]init];
    [line setBackgroundColor:HexRGBA(0xe9e9e9, 1)];
    [phoneView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(phoneView);
        make.height.mas_equalTo(0.5);
    }];
    
    
    UIButton * forgetButton = [[UIButton alloc]init];
    [forgetButton setTitle:@"忘记密码？" forState:UIControlStateNormal];
    [forgetButton.titleLabel setFont:[UIFont systemFontOfSize:13 * kAutoScanleX]];
    [forgetButton setTitleColor:HexRGBA(0x424448, 1) forState:UIControlStateNormal];
    [forgetButton addTarget:self action:@selector(buttonForgetAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:forgetButton];
    [forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(midView).offset(-25 * kAutoScanleX);
        make.top.equalTo(midView.mas_bottom);
        make.height.mas_equalTo(40 * kAutoScanleX);
    }];
    
    
    UIButton * loginBtn=[[UIButton alloc]init];
    [loginBtn setBackgroundColor:HexRGBA(0xb99c6d, 1)];
    [loginBtn addTarget:self action:@selector(loginBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [loginBtn.layer setMasksToBounds:YES];
    [loginBtn.layer setCornerRadius:8.0 * kAutoScanleX];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn.titleLabel setFont:[UIFont systemFontOfSize:16 * kAutoScanleX]];
    [self.view addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(forgetButton.mas_bottom).offset(40 * kAutoScanleX);
        make.height.mas_equalTo(45 * kAutoScanleX);
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(576/2.0 * kAutoScanleX);
    }];
    
    
    typeButton = [[UIButton alloc]init];
    [typeButton setTitle:@"使用密码登录" forState:UIControlStateNormal];
    [typeButton setTitle:@"使用验证码登录" forState:UIControlStateSelected];
    [typeButton.titleLabel setFont:[UIFont systemFontOfSize:13 * kAutoScanleX]];
    [typeButton setTitleColor:HexRGBA(0xb99c6d, 1) forState:UIControlStateNormal];
    [typeButton addTarget:self action:@selector(buttonTypeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:typeButton];
    [typeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loginBtn.mas_bottom).offset(15 * kAutoScanleX);
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(40 * kAutoScanleX);
    }];

}

#pragma mark--获取验证码
-(void)buttonAction:(UIButton *)sender{
    
    
    [ApplicationDelegate.window endEditing:YES];
    
    if (phoneTextField.text.length!=11) return [self showHint:@"请核对您的手机号"];
    
    WS(weakSelf);
    
    [weakSelf setAuthCodeSuccess];
    
    
    [HttpRequestHelper requestApiAuthCodeMobile:phoneTextField.text check:1 Block:^(NSString *code, NSString *msg) {

        if (REQUEST_SUCCESS) {

            NSLog(@"code : %@",code);

        }else{

            [weakSelf showHint:msg];
        }
    }];
    
}

-(void)setAuthCodeSuccess{
    
    count = 60;
    
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reduceTime:) userInfo:nil repeats:YES];
    
//    [self showHint:@"验证码已经发送到您的手机"];
    
}

#pragma mark -- 查看密码
- (void)buttonSecureAction:(UIButton *)sender{
    
    sender.selected = !sender.selected;
    [passWordTextField setSecureTextEntry:!sender.selected];
    
}

#pragma mark--定时器
-(void)reduceTime:(NSTimer *)_timer{
    
    count--;
    
    count == 0 ? [self labelNormal] : [self labelSelected];
    
}
-(void)labelNormal{
    
    [label setText:@"获取验证码"];
    [codeBtn setUserInteractionEnabled:YES];
    [timer invalidate];
    
}
-(void)labelSelected{
    
    
    [label setText:[NSString stringWithFormat:@"%ld s",count]];
    [codeBtn setUserInteractionEnabled:NO];
    
}


- (void)loginBtnAction:(UIButton *)sender{
    
    [self.view endEditing:YES];
    
    WS(weakSelf);
    
    NSString * mobile = phoneTextField.text;
    NSString * password = passWordTextField.text;
    NSString * vcode = codeTextField.text;
    NSString * type = @"";
    
    if (mobile.length != 11) {
        
        return [self showHint:@"请填写正确手机号"];
    }
    
    if (typeButton.selected) {
        
        type = @"password";
        if (password.length == 0) return [self showHint:@"请填写密码"];
        
    }else{
        
        type = @"vcode";
        if(vcode.length == 0) return [self showHint:@"请填写验证码"];
    }
    
    
    
    [self showHudInView:self.view hint:@""];
    [HttpRequestHelper requestApiLoginMobile:mobile password:password type:type vcode:vcode Block:^(NSString *msg, NSString *infoMsg,UserModel * model) {
        [weakSelf hideHud];
        if (REQUEST_SUCCESS) {
            
            [weakSelf myLoginFinish];
            
        }else{
            
            [weakSelf showHint:msg];
        }
    }];
    
   
    
}

#pragma mark -- 完成
- (void)myLoginFinish{
    
    NSString * token = [UserCenterHelper shareInstance].token;
    NSDictionary * dic = [UserCenterHelper shareInstance].userModel.userDic;
    
    [self backAction:nil];
    
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    [defaults setObject:token forKey:@"token"];
    [defaults setObject:dic forKey:USER_DIC];
    [defaults synchronize];
    
    
    if (self.loginFinish) {
        
        self.loginFinish();
    }
}



- (void)buttonForgetAction:(UIButton *)sender{
    
    
    ForgetPasswordViewController * vc = [[ForgetPasswordViewController alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark -- 切换登录方式
- (void)buttonTypeAction:(UIButton *)sender{
   
    sender.selected = !sender.selected;
    
    if (sender.selected) {
        
        [codeView setHidden:YES];
        [passView setHidden:NO];
        
    }else{
        
        [codeView setHidden:NO];
        [passView setHidden:YES];
    }
   
  
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.view endEditing:YES];
    
    return YES;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
    
}

#pragma mark -- 注册
- (void)registerBtnAction:(UIButton *)sender{
    
    RegisterViewController * vc = [[RegisterViewController alloc]init];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}


@end
